PDF is one of the most commonly used formats in people’s daily work. So converting word to pdf is an important issue. When people face the problem of converting word to pdf, they may choose to buy conversion software. But for Spire.Doc users, you need not use any conversion software. Spire.Doc enables its users to easily convert word to pdf and other different kinds of formats, such as word to xml, tiff image, rtf, html, epub and so on. Furthermore, Spire.Doc supports .NET and Silverlight platform to perform a wide range o processing tasks directly without Microsoft Office Word installed in your computer. This article is designed to teach you how to convert word to pdf with C#, VB.NET in Spire.Doc with Visual Studio. Before you start, please make sure that Visual Studio and Spire.Doc are installed in your computer.
Freely Download Spire.Doc
Freely Download Spire.Doc
Procedure
First, create the Word to PDF project.
Open Visual Studio (2008 or later)—File—New Project—Visual C#—Console Application. Name Console Application Word to PDF(if you need to create a Visual Basic project, Console Application, you can name the project Word to PDF)—OK.
Second, add references.
Open Solution Explorer—Click project —Add Reference—Find "C:\Program Files\e-iceblue\Spire.Doc"(the default folder which you installed the Spire Doc in )— Double-click the folder Bin.
If the target framework of the project
Net 2.0, double-click folder NET2.0
or Net 3.5, double-click folder NET3.5
or Net 4.0, double-click folder NET4.0
Select assembly Spire Doc dll —Click OK
Third, add the following codes to the top of the file:
C# Code:
using Spire.Doc;
using Spire.Doc. Documents;
using Spire.Doc.Fields;
VB.NET Code:
Imports Spire.Doc;
Imports Spire.Doc. Documents;
Imports Spire.Doc.Fields;
C# Code:
using Spire.Doc;
using Spire.Doc. Documents;
using Spire.Doc.Fields;
VB.NET Code:
Imports Spire.Doc;
Imports Spire.Doc. Documents;
Imports Spire.Doc.Fields;
Fourth, add the following codes to the method.
C# Code:
//Create word document
Document document = new Document();
Section section = document.AddSection();
section.PageSetup.PageSize = PageSize.A4;
section.PageSetup.Margins.Top = 72f;
section.PageSetup.Margins.Bottom = 72f;
section.PageSetup.Margins.Left = 89.85f;
section.PageSetup.Margins.Right = 89.85f;
String p1
= "Microsoft Word is a word processor designed by Microsoft. "
+ "It was first released in 1983 under the name Multi-Tool Word for Xenix systems. "
+ "Subsequent versions were later written for several other platforms including "
+ "IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (195), "
+ "Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989). ";
String p2
= "Microsoft Office Word instead of merely Microsoft Word. "
+ "The 2010 version appears to be branded as Microsoft Word, "
+ "once again. The current versions are Microsoft Word 2010 for Windows and 2008 for Mac.";
section.AddParagraph().AppendText(p1).CharacterFormat.FontSize = 14;
section.AddParagraph().AppendText(p2).CharacterFormat.FontSize = 14;
//Save doc file to pdf.
document.SaveToFile("Sample.pdf", FileFormat.PDF);
C# Code:
//Create word document
Document document = new Document();
Section section = document.AddSection();
section.PageSetup.PageSize = PageSize.A4;
section.PageSetup.Margins.Top = 72f;
section.PageSetup.Margins.Bottom = 72f;
section.PageSetup.Margins.Left = 89.85f;
section.PageSetup.Margins.Right = 89.85f;
String p1
= "Microsoft Word is a word processor designed by Microsoft. "
+ "It was first released in 1983 under the name Multi-Tool Word for Xenix systems. "
+ "Subsequent versions were later written for several other platforms including "
+ "IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (195), "
+ "Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989). ";
String p2
= "Microsoft Office Word instead of merely Microsoft Word. "
+ "The 2010 version appears to be branded as Microsoft Word, "
+ "once again. The current versions are Microsoft Word 2010 for Windows and 2008 for Mac.";
section.AddParagraph().AppendText(p1).CharacterFormat.FontSize = 14;
section.AddParagraph().AppendText(p2).CharacterFormat.FontSize = 14;
//Save doc file to pdf.
document.SaveToFile("Sample.pdf", FileFormat.PDF);
VB.NET Code:
'Create word document
Dim document As New Document()
Dim section As Section = document.AddSection()
section.PageSetup.PageSize = PageSize.A4
section.PageSetup.Margins.Top = 72.0F
section.PageSetup.Margins.Bottom = 72.0F
section.PageSetup.Margins.Left = 89.85F
section.PageSetup.Margins.Right = 89.85F
Dim paragraph As Paragraph = section.AddParagraph()
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left
paragraph.AppendPicture(Image.FromFile("Word.png"))
Dim p1 As String _
= "Microsoft Word is a word processor designed by Microsoft. " _
+ "It was first released in 1983 under the name Multi-Tool Word for Xenix systems. " _
+ "Subsequent versions were later written for several other platforms including " _
+ "IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), " _
+ "Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989). "
Dim p2 As String _
= "Microsoft Office Word instead of merely Microsoft Word. " _
+ "The 2010 version appears to be branded as Microsoft Word, " _
+ "once again. The current versions are Microsoft Word 2010 for Windows and 2008 for Mac."
section.AddParagraph().AppendText(p1).CharacterFormat.FontSize = 14
section.AddParagraph().AppendText(p2).CharacterFormat.FontSize = 14
'Save doc file to pdf.
document.SaveToFile("Sample.pdf", FileFormat.PDF)
Finally, operate the project Word to PDF.
In Solution Explorer, right-click the project Word to PDF —Click Debug—Start new instance, you can see the picture 5.1 in your project:
In Solution Explorer, right-click the project Word to PDF —Click Debug—Start new instance, you can see the picture 5.1 in your project:
Converting Word to PDF with C#,VB.NET in Spire. Doc is no longer an issue for Spire.Doc users since you have Spire.Doc by your side to help you. Using Spire. Doc, people can publish electronic document and deliver digital information on internet in that it does not connected with operation system. You can use PDF file format to package font, format, color, image, hyperlink, voice and video with high security and integration. Finally, the method talked above is not the only method to convert word to pdf for C#, VB.NET in Spire.Doc. The other method can be shown in How to Convert Word to PDF.
Source